﻿/*
ajaxBookSort32上传笔记本排序
ajaxAreaSort32上传分区排序
ajaxPageSort32上传页排序
ajaxLoadData32加载笔记本、分区、页数据
ajaxPage32加载页的数据
ajaxRegion32加载域的数据
ajaxAddBook32创建笔记本及一个默认分区
ajaxAddArea32创建分区
ajaxAddPage32创建页
ajaxSaveRegion32保存页内容
ajaxThrowPage32回收页
*/



(function ($) {
    /*--------------------------------------------------上传笔记本排序*/
    /*--------------------------------------------------使用者是$("#bookList")*/
    $.fn.ajaxBookSort32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this);
            var sort = "";
            JQself.find(".book").each(function () {
                sort = sort + "-" + $(this).idAnaly_();
            });
            sort = sort.substring(1);
            $.Ajax32.editBookSort({
                params: {
                    sort: sort
                },
                callback: options.callback
            });
        });
    },

    /*--------------------------------------------------上传分区排序*/
    /*--------------------------------------------------使用者是$("#bookList").find(".areaList")*/
    $.fn.ajaxAreaSort32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this);
            var sort = "";
            JQself.find(".area").each(function () {
                sort = sort + "-" + $(this).idAnaly_();
            });
            sort = sort.substring(1);
            var id = JQself.find(".area").idAnaly_({ toward: "father" });
            $.Ajax32.editAreaSort({
                params: {
                    notebookID: id,
                    areaSort: sort
                },
                callback: options.callback
            });
        });
    },

    /*--------------------------------------------------上传页排序*/
    /*--------------------------------------------------使用者是$("#pageList")*/
    $.fn.ajaxPageSort32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this);
            var sort = "";
            JQself.find(".page").each(function () {
                sort = sort + "-" + $(this).idAnaly_();
            });
            sort = sort.substring(1);
            var id = JQself.find(".page").idAnaly_({ toward: "father" });
            $.Ajax32.editPageSort({
                params: {
                    sort: sort,
                    id: id
                },
                callback: options.callback
            });

        });
    },

    /*--------------------------------------------------获取笔记本、分区、页数据*/
    /*--------------------------------------------------使用者是$("#bookList")*/
    $.fn.ajaxLoadData32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            $.Ajax32.getLoadData({
                callback: function (data) {
                    //--------------笔记本
                    var JQbookList = $("#bookList");
                    var bookList = data.NoteBookList; //笔记本集合
                    var bookSort = data.NoteBookSort.split('-'); //笔记本排序
                    JQbookList.empty();
                    for (var i in bookSort) {
                        for (var j = 0; j < bookList.length; j++) {
                            if (bookSort[i] == bookList[j].ID) {
                                var JQbook = $('<div class="book"><div class="head"><span class="title"></span><div class="arrow"></div></div><div class="areaList"></div></div>').appendTo(JQbookList);
                                JQbook.attr("id", "book_" + bookList[j].ID);
                                JQbook.find(".head .title").text(bookList[j].Name);
                            }
                        }
                    }


                    //--------------分区

                    var areaList = data.AreaList; //所有分区集合
                    for (var i = 0; i < bookList.length; i++) {
                        JQbookList.find(".book").each(function () {
                            if ($(this).idAnaly_({ toward: "head" }) == "book" && $(this).idAnaly_() == bookList[i].ID) {
                                var JQbook = $(this); //对应的笔记本元素
                                var areaList2 = new Array(); //对应的分区集合
                                if (bookList[i].AreaSort == null) {
                                    return;
                                }
                                var areaSort = bookList[i].AreaSort.split('-'); //对应的分区排序
                                for (var j = 0; j < areaList.length; j++) {
                                    if (areaList[j].NoteBookID == JQbook.idAnaly_()) {
                                        areaList2.push(areaList[j]);
                                    }
                                }
                                for (var j in areaSort) {
                                    for (var z = 0; z < areaList2.length; z++) {
                                        if (areaSort[j] == areaList2[z].ID) {
                                            var JQareaList = JQbook.find(".areaList");
                                            var JQarea = $('<div class="area"><div class="icon"></div><span class="title"></span></div>');
                                            JQarea.appendTo(JQareaList);
                                            JQarea.attr("id", "area_" + areaList2[z].ID + "_" + bookList[i].ID);
                                            JQarea.find(".title").text(areaList2[z].Name);
                                        }
                                    }
                                }
                            }
                        });
                    }
                    //如果数据笔记本表没有排序字段


                    $("#bookList").find(".area").area32();
                    $("#bookList").find(".book").book32();
                    $("#bookList").find(".head:not(:eq(0))").click();

                    //--------------页
                    var JQpageList = $("#pageList");
                    var pageList = data.PageList; //页集合
                    var pageSort = data.AreaList[0].PageSort.split('-'); //页排序
                    JQpageList.empty();
                    for (var i in pageSort) {
                        for (var j = 0; j < pageList.length; j++) {
                            if (pageSort[i] == pageList[j].ID) {
                                var JQpage = $('<div class="page"><div class="bg"></div><span class="title"></span><span class="date"></span><div class="line"></div></div>').appendTo(JQpageList);
                                JQpage.attr("id", "page_" + pageList[j].ID + "_" + pageList[j].AreaID);
                                JQpage.find(".title").text(pageList[j].Name);
                                JQpage.find(".date").text($.Json_.changeTime({ data: pageList[j].CreateTime }));
                            }
                        }
                    }

                    $("#pageList").find(".page").page32();

                    if ($("#bookList").find(".area:eq(0)")) {
                        $("#currentArea").val($("#bookList").find(".area:eq(0)").idAnaly_());
                    }
                    if ($("#pageList").find(".page:eq(0)")) {
                        $("#currentPage").val($("#pageList").find(".page:eq(0)").idAnaly_());
                    }

                    options.callback();
                }
            });

        });
    },

    /*--------------------------------------------------加载页的数据*/
    /*--------------------------------------------------使用者是$("#pageList")*/
    $.fn.ajaxPage32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var $pageList = $(this);
            $pageList.empty();

            $.Ajax32.getPage({
                params: {
                    areaID: options.params.areaID,
                    pageIndex: options.params.pageIndex,
                    pageSize: options.params.pageSize,
                    sortType: options.params.sortType
                },
                callback: function (data) {
                    var pageList = data.PageList; //页集合
                    for (var i = 0; i < pageList.length; i++) {
                        var JQpage = $('<div class="page"><div class="bg"></div><span class="title"></span><span class="date"></span><div class="line"></div></div>').appendTo($pageList);
                        JQpage.attr("id", "page_" + pageList[i].ID + "_" + pageList[i].AreaID);
                        JQpage.find(".title").text(pageList[i].Name);
                        JQpage.find(".date").text($.Json_.changeTime({ data: pageList[i].CreateTime }));
                    }

                    $("#pageList").find(".page").page32();
                    options.callback();
                }
            });

        });
    },

    /*--------------------------------------------------创建笔记本及一个默认分区*/
    /*--------------------------------------------------使用者是$("#bookList")*/
    $.fn.ajaxAddBook32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQbookList = $(this);

            var JQbook = $('<div class="book"><div class="head"><span class="title"></span><div class="arrow"></div></div><div class="areaList"></div></div>');
            var JQareaList = JQbook.find(".areaList");
            JQbook.prependTo(JQbookList);
            JQbook.find(".head .title").text("新建笔记本");
            var JQarea = $('<div class="area"><div class="icon"></div><span class="title"></span></div>');
            JQarea.prependTo(JQareaList);
            JQarea.find(".title").text("默认分区");

            JQbook.book32();
            JQarea.area32();

            JQbook.find(".head .title").reName32({
                className: "renameBookTextBox",
                focusout: function (ui32) {
                    $.Ajax32.addBook({
                        params: {
                            notebookName: ui32.newName
                        },
                        callback: function (data) {
                            JQbook.attr("id", "book_" + data.NoteBookID);
                            JQarea.attr("id", "area_" + data.AreaID + "_" + data.NoteBookID); //这个要修改。。。。。。。。。。。。。。。。
                            $("#bookList").ajaxBookSort32();
                            JQbook.find(".areaList").ajaxAreaSort32();
                        }
                    });
                }
            });
        });
    },

    /*--------------------------------------------------在该笔记本里创建分区*/
    /*--------------------------------------------------使用者是$("#bookList").find(".areaList")*/
    $.fn.ajaxAddArea32 = function (options) {
        var defaults = {
            callback: function () { } //回调函数
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this);
            JQself.show();
            var JQarea = $('<div class="area"><div class="icon"></div><span class="title">新建分区</span></div>').prependTo(JQself);
            JQarea.find(".icon").followParent32({ left: 16, middle: true });
            JQarea.find(".title").followParent32({ left: 42, top: 7.5 }).text("新建分区"); alert(JQself.parents(".book").idAnaly_());
            JQarea.find(".title").reName32({
                className: "renameAreaTextBox",
                focusout: function (ui32) {
                    $.Ajax32.addArea({
                        params: {
                            notebookID: JQself.parents(".book").idAnaly_(),
                            areaName: ui32.newName
                        },
                        callback: function (data) {
                            ui32.rename();
                            JQarea.attr("id", "area_" + data.AreaID + "_" + JQself.parent(".book").idAnaly_());
                            JQarea.parents(".areaList").ajaxAreaSort32();
                            options.callback();
                        }
                    });
                }
            });
        });
    },


    /*--------------------------------------------------创建页*/
    /*--------------------------------------------------使用者是$("#pageList")*/
    $.fn.ajaxAddPage32 = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);
        return this.each(function () {
            var JQself = $(this);
            var JQpage = $('<div class="page"><div class="bg"></div><span class="title">新建页</span><span class="date"></span><div class="line"></div></div>');
            JQpage.prependTo($("#pageList"));
            JQpage.page32();

            var pageContent = '<div id="regionInner"></div>';
            pageContent = escape(pageContent);

            JQpage.find(".title").reName32({
                className: "renamePageTextBox",
                focusout: function (ui32) {
                    $.Ajax32.addPage({
                        params: {
                            areaID: options.params.areaID,
                            pageName: ui32.newName,
                            pageContent: pageContent
                        },
                        callback: function (data) {
                            ui32.rename();
                            JQpage.attr("id", "page_" + data.PageID + "_" + options.params.areaID);
                            $("#pageList").ajaxPageSort32();
                        }
                    });
                }
            });
        });
    },


    /*--------------------------------------------------保存域，即页的内容*/
    /*--------------------------------------------------使用者是$("#region")*/
    $.fn.ajaxSaveRegion32 = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);
        return this.each(function () {
            var $region = $(this);

            var pageID = $("#currentPage").val();
            if (pageID == "") {
                return false;
            }


            $("#regionInner").find(".piece").sortable('destroy');
            $("#regionInner").find(".piece").draggable('destroy');
            $("#regionInner").find(".piece").resizable('destroy');
            $("#regionInner").droppable('destroy');


            //alert($region.html());
            //var pageContent = encodeURIComponent($region.html()); //要对html转码
            var pageContent = escape($region.html());

            //alert(pageContent.length);

            $.Ajax32.renewRegion({
                params: {
                    pageID: "6",
                    pageContent: pageContent
                },
                callback: options.callback
            });



        });
    },


    /*--------------------------------------------------更换域，换上别的页的内容*/
    /*--------------------------------------------------使用者是$("#region")*/
    $.fn.ajaxRegion32 = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);
        return this.each(function () {
            var JQself = $(this);

            var pageID = $("#currentPage").val();

            if (pageID == "") {
                return false;
            }

            $.Ajax32.getRegion({
                params: {
                    pageID: pageID
                },
                callback: function (data) {
                    //$("#region").html(unescape(data.Content).replace("%2",""));
                    //$("#region").html(data.Content);

                    $("#region").empty();

                    //alert(data.Content.length);
                    $("#region").html(unescape(data.Content));
                    //$("#region").html(decodeURIComponent(data.Content));
                    //alert($("#region").html());

                    $("#regionInner").find(".piece").piece32({ active: true });
                    $("#regionInner").droppable({
                        scope: "pieces",
                        drop: function (e, ui) {
                            var $regionInner = $(this);
                            var $defaults = $("#defaultPieces"); //笔记块模板集合
                            var $clone; //被克隆出来的对象
                            if (ui.helper.hasClass("imgBuildWrite")) {
                                $clone = $defaults.find(".Write").clone();
                            } else if (ui.helper.hasClass("imgBuildDraw")) {
                                $clone = $defaults.find(".draw").clone();
                            } else if (ui.helper.hasClass("imgBuildPhoto")) {
                                $clone = $defaults.find(".photo").clone();
                            } else if (ui.helper.hasClass("imgBuildRecord")) {
                                $clone = $defaults.find(".record").clone();
                            } else if (ui.helper.hasClass("imgBuildPicture")) {
                                $clone = $defaults.find(".picture").clone();
                            } else if (ui.helper.hasClass("imgBuildFile")) {
                                $clone = $defaults.find(".file").clone();
                            }
                            $clone.show().appendTo($regionInner);
                            $clone.followParent32({
                                left: e.clientX - $("#regionInner").offset().left,
                                top: e.clientY - $("#regionInner").offset().top
                            });
                            $clone.piece32({ active: true });
                            $clone.mousedown();
                        }
                    });

                }
            });

            var pageContent = escape(JQself.html()); //要对html转码


        });
    },


    /*--------------------------------------------------回收页*/
    /*--------------------------------------------------使用者是$("#pageList").find(".area")*/
    $.fn.ajaxThrowPage32 = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);
        return this.each(function () {
            var JQself = $(this);

            var pageID = JQself.idAnaly_();

            $.Ajax32.throwPage({
                params: {
                    pageID: pageID
                },
                callback: options.callback
            });



        });
    },


    /*--------------------------------------------------共享页*/
    /*--------------------------------------------------使用者是$("#region")或者任何对象*/
    $.fn.ajaxThrowPage32 = function (options) {
        var defaults = {
            pageID: ""
        };
        var options = $.extend(defaults, options);
        return this.each(function () {
            var JQself = $(this);

            var pageID = $("#currentPage").val();

            $.Ajax32.sharePage({
                params: {
                    pageID: pageID
                },
                callback: options.callback
            });



        });
    }


})(jQuery);  

